package com.lhkj.ct.meta.modules.psychology.service.impl;

import com.lhkj.ct.base.utils.Validate;
import com.lhkj.ct.base.utils.file.word.PoiWordUtils;
import com.lhkj.ct.meta.modules.gauge.mapper.ScaleDimensionMapper;
import com.lhkj.ct.meta.modules.gauge.mapper.ScaleMapper;
import com.lhkj.ct.meta.modules.gauge.model.entity.TblScale;
import com.lhkj.ct.meta.modules.psychology.model.bo.DimensionBo;
import com.lhkj.ct.meta.modules.psychology.model.dto.SampleAnalysisDto;
import com.lhkj.ct.meta.modules.psychology.model.vo.AnalysisResult;
import com.lhkj.ct.meta.modules.psychology.service.GaugeAnswerService;
import com.lhkj.ct.meta.modules.psychology.service.SampleAnalysisService;
import com.lhkj.ct.meta.modules.report.BaseFactory;
import com.lhkj.ct.meta.modules.report.word.group.GaugeStatisticsReportHandle;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *     样本分析
 * </p>
 */
@Service
public class SampleAnalysisServiceImpl implements SampleAnalysisService {

    @Resource
    private ScaleMapper scaleMapper;

    @Resource
    private ScaleDimensionMapper dimensionMapper;

    @Resource
    private GaugeAnswerService answerService;

    @Override
    public AnalysisResult statistical(SampleAnalysisDto dto) {
        AnalysisResult vo = new AnalysisResult();
        // step 1.查询量表信息
        TblScale scale = scaleMapper.selectById(dto.getScaleId());
        Validate.notNull(scale, "量表不存在或已被删除");
        vo.setScaleName(scale.getScaleName());
        // step 2.查询量表所包含的维度，并生成维度的计算公式
        List<DimensionBo> dimensions = dimensionMapper.selectAllByScaleId(scale.getId())
                .stream().peek(DimensionBo::builderFormula).collect(Collectors.toList());
        // step 3.计算团体包含的报告在各个维度得分、并生成描述统计和差异分析数据
        vo.generateStatisticalDescription(dto.getCombines(), dimensions, vo,scale, (rId, gaugeId)->
                answerService.convertAnswers2FormData(rId, gaugeId));
        // step 4.报告结论
        List<String> conclusions = new ArrayList<>();
        for (AnalysisResult.Diversity diversity : vo.getDiversityList()) {
            if (diversity.getPValue() > 0.05) {
                conclusions.add("在" + diversity.getDimenName() + "上得分差异不显著(F=" + diversity.getF() + "," + diversity.getP() + ")");
            }
            // tips 因为P有显示P = 0.05的情况，所以这里不能去除，也可以用Aviator处理可去除这个判断
            else if (diversity.getPValue() == 0.05) {
                conclusions.add("在" + diversity.getDimenName() + "上得分差异显著(F=" + diversity.getF() + "," + diversity.getP() + ")");
            }
            else {
                conclusions.add("在" + diversity.getDimenName() + "上得分差异显著(F=" + diversity.getF() + "," + diversity.getP() + ")");
            }
        }
        vo.setConclusion(conclusions.stream().collect(Collectors.joining(",","各团体间","")));
        return vo;
    }

    @Override
    public void exportWord(AnalysisResult vo) {
        XWPFDocument document = BaseFactory.getReport(new GaugeStatisticsReportHandle(), vo);
        PoiWordUtils.wordDownload(document, "样本分析.docx");
    }
}
